package jp.eisbahn.mgapi.auth;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * This class has information about the configuration to use mixi Graph API.
 * @author Yoichiro Tanaka
 */
public class ClientConfig {

    /** The client ID. */
    private String clientId;
    /** The client secret. */
    private String clientSecret;
    /** The redirect URI. */
    private String redirectUri;

    /**
     * Initialize this instance.
     */
    public ClientConfig() {
        super();
    }

    /**
     * Initialize this instance.
     * @param newClientId The client ID.
     * @param newClientSecret The client secret.
     * @param newRedirectUri The redirect URI.
     */
    public ClientConfig(
            final String newClientId,
            final String newClientSecret,
            final String newRedirectUri) {
        super();
        clientId = newClientId;
        clientSecret = newClientSecret;
        redirectUri = newRedirectUri;
    }

    /**
     * Set the client ID.
     * @param newClientId The client ID.
     */
    public final void setClientId(final String newClientId) {
        clientId = newClientId;
    }

    /**
     * Retrieve the client ID.
     * @return The client ID.
     */
    public final String getClientId() {
        return clientId;
    }

    /**
     * Set the client secret.
     * @param newClientSecret The client secret string.
     */
    public final void setClientSecret(final String newClientSecret) {
        clientSecret = newClientSecret;
    }

    /**
     * Retrieve the client secret.
     * @return The client secret string.
     */
    public final String getClientSecret() {
        return clientSecret;
    }

    /**
     * Set the redirect URI.
     * @param newRedirectUri The redirect URI string.
     */
    public final void setRedirectUri(final String newRedirectUri) {
        redirectUri = newRedirectUri;
    }

    /**
     * Retrieve the redirect URI.
     * @return The redirect URI.
     */
    public final String getRedirectUri() {
        return redirectUri;
    }

    /**
     * Load the configuration information from the resource
     * specified by the path.
     * @param path The path to locate the resource which has the configuration.
     * @throws IOException If some errors occurred while loading the resource.
     */
    public final void loadFromResource(final String path) throws IOException {
        Properties properties = new Properties();
        InputStream in = ClientConfig.class.getResourceAsStream(path);
        if (in == null) {
            throw new FileNotFoundException(path + " not found in Classpath.");
        }
        properties.load(in);
        setClientId(properties.getProperty("client_id"));
        setClientSecret(properties.getProperty("client_secret"));
        setRedirectUri(properties.getProperty("redirect_uri"));
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public final int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + getHashCode(clientId);
        result = prime * result + getHashCode(clientSecret);
        result = prime * result + getHashCode(redirectUri);
        return result;
    }

    /**
     * Retrieve the hash code.
     * @param obj The object want to get.
     * @return The hash code. If the obj is null, return 0;
     */
    private int getHashCode(final String obj) {
        if (obj != null) {
            return obj.hashCode();
        } else {
            return 0;
        }
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public final boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        ClientConfig other = (ClientConfig) obj;
        if (clientId == null) {
            if (other.clientId != null) {
                return false;
            }
        } else if (!clientId.equals(other.clientId)) {
            return false;
        }
        if (clientSecret == null) {
            if (other.clientSecret != null) {
                return false;
            }
        } else if (!clientSecret.equals(other.clientSecret)) {
            return false;
        }
        if (redirectUri == null) {
            if (other.redirectUri != null) {
                return false;
            }
        } else if (!redirectUri.equals(other.redirectUri)) {
            return false;
        }
        return true;
    }

}
